package org.geektimes.configuration.microprofile.config.source;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ApplicationPropertiesConfigSource extends MapBasedConfigSource {

    Logger logger = Logger.getLogger(ApplicationPropertiesConfigSource.class.getName());

    private static final String configFileLocation = "META-INF/application.properties";

    public ApplicationPropertiesConfigSource(){
        super("application.properties",500);
    }

    @Override
    protected void prepareConfigData(Map configData) throws Throwable {
        ClassLoader classLoader = getClass().getClassLoader();
        try(InputStream inputStream = classLoader.getResourceAsStream(configFileLocation)){
            Properties props = new Properties();
            props.load(inputStream);
            configData.putAll(props);
        } catch (IOException e) {
            logger.log(Level.SEVERE,"读取配置文件失败");
            throw new RuntimeException("读取配置文件失败");
        }
    }

}
